package com.chenqq.camerademo.camera.helper;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public class CameraTools  {
    private CameraTools(){
        compositeDisposable=new CompositeDisposable();
    }
    private static CameraTools inStance;
    private CompositeDisposable compositeDisposable;
    private  HashMap<String, Disposable> disposableHashMap = new HashMap<>();
    public static CameraTools getInStance() {
        if (inStance==null){
            inStance=new CameraTools();
        }
        return inStance;
    }

    public  void addDisposable(String tag,Disposable disposable){
        disposableHashMap.put(tag,disposable);
        compositeDisposable.add(disposable);
    }

    public void removeDisposable(String tag){
        Disposable disposable = disposableHashMap.get(tag);
        if (disposable!=null){
            compositeDisposable.remove(disposable);
            disposableHashMap.remove(tag);
        }
    }

    public void cleanAllDisposable(){
        Iterator<Map.Entry<String, Disposable>> iterator = disposableHashMap.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, Disposable> next = iterator.next();
            compositeDisposable.remove(next.getValue());
        }
        disposableHashMap.clear();
    }
}
